# project
# ******************************************************************************
cmake_minimum_required(VERSION 3.0)

set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake;")
include(Functions)

set(TEMPLATE_APPLICATION_NAME "QMeshFix" CACHE STRING "Application name")
set(PROJECT_NAME ${TEMPLATE_APPLICATION_NAME})
project(${PROJECT_NAME})


add_definitions(-DMODEL_TRIANGLE3D) #定义模型文件类型
add_definitions(-DFUNC_MESHFIX)
# add_definitions(-D__WIN32__)
set(CMAKE_DEBUG_POSTFIX "d")

set(TEMPLATE_USE_CPP11 ON CACHE BOOL "Use C++11")

# project directories
# 项目目录
# ******************************************************************************
#set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/install)
set(ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
set(PROJECT_INSTALL_DIR ${CMAKE_INSTALL_PREFIX})
set(BUILD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/build)

# output directories
# 输出目录
# ******************************************************************************
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${BUILD_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${BUILD_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${BUILD_DIR})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${BUILD_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${BUILD_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${BUILD_DIR})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${BUILD_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${BUILD_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${BUILD_DIR})

# meta
# ******************************************************************************
set(TEMPLATE_VERSION_MAJOR 2 CACHE INT "Version Major")
set(TEMPLATE_VERSION_MINOR 0 CACHE INT "Version Minor")
set(TEMPLATE_VERSION_PATCH 0 CACHE INT "Version Patch")
set(TEMPLATE_ORGANIZATION "organization" CACHE STRING "Organization")
set(TEMPLATE_DOMAIN "domain" CACHE STRING "Domain")

# Qt
# ******************************************************************************

set(TEMPLATE_QT_MODULES Core Gui Widgets Quick Qml Concurrent Network SerialPort PrintSupport)
find_package(QT NAMES Qt6 Qt5 COMPONENTS ${TEMPLATE_QT_MODULES} REQUIRED)
find_package(Qt${QT_VERSION_MAJOR}  CONFIG REQUIRED ${TEMPLATE_QT_MODULES})
set(CMAKE_AUTOMOC true)

# configure files
# ******************************************************************************
configure_file(${ROOT_DIR}/cmake/meta.hpp.in ${BINARY_DIR}/meta.hpp)

if(APPLE)
    configure_file(${ROOT_DIR}/cmake/info.plist.in ${BINARY_DIR}/info.plist)
endif()

# definitions
# ******************************************************************************
add_definitions(${QT_DEFINITIONS})
add_definitions(-DUSE_INSTALL_TARGET)

# apple rpath
# ******************************************************************************
if(APPLE)
    set(CMAKE_SKIP_BUILD_RPATH false)
    set(CMAKE_BUILD_WITH_INSTALL_RPATH false)
    set(CMAKE_INSTALL_RPATH "@executable_path/../Frameworks")
    set(CMAKE_INSTALL_RPATH_USE_LINK_PATH true)
endif()

#message("SB------>${SOURCE_DIR}")
# sources
# ******************************************************************************
AddSources("${SOURCE_DIR}/include" "include" list_app_include)
AddSources("${SOURCE_DIR}/src" "src" list_app_src)
# AddSources("${SOURCE_DIR}/include/MeshFix" "include/MeshFix" list_app_include)
# AddSources("${SOURCE_DIR}/include/TMesh" "include/TMesh" list_app_include)

# AddSources("${SOURCE_DIR}/src/Algorithms" "src/Algorithms" list_app_src)
# AddSources("${SOURCE_DIR}/src/Kernel" "src/Kernel" list_app_src)
# AddSources("${SOURCE_DIR}/src/MeshFix" "src/MeshFix" list_app_src)
# AddSources("${SOURCE_DIR}/src/TMesh" "src/TMesh" list_app_src)

# AddResources("${ROOT_DIR}/Resources/qml" "Qml" list_qml_src)
# AddResourcesQRC("${SOURCE_DIR}" "Resources" list_Resources_src)
message("list_app_include------>${list_app_include}")
#message("SB:" +${ROOT_DIR})
# target
# ******************************************************************************
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(ALL_SOURCES
    ${list_app_src}
    ${list_app_include}
    )
if(APPLE)
    set(MACOSX_BUNDLE_BUNDLE_VERSION 1)
    set(MACOSX_BUNDLE_LONG_VERSION_STRING ${TEMPLATE_VERSION_MAJOR}.${TEMPLATE_VERSION_MINOR}.${TEMPLATE_VERSION_PATCH})
    set(MACOSX_BUNDLE_SHORT_VERSION_STRING ${TEMPLATE_VERSION_MAJOR}.${TEMPLATE_VERSION_MINOR}.${TEMPLATE_VERSION_PATCH})
    set(MACOSX_BUNDLE_ICON_FILE Icon.icns)
    set(MACOSX_BUNDLE_BUNDLE_NAME ${PROJECT_NAME})
    set(MACOSX_BUNDLE_RESOURCES ${BUILD_DIR}/${PROJECT_NAME}.app/Contents/Resources)
    set(MACOSX_BUNDLE_ICON ${ROOT_DIR}/Resources/icons/develop/${MACOSX_BUNDLE_ICON_FILE})

    #add_library(${PROJECT_NAME}  ${ALL_SOURCES} ${MACOSX_BUNDLE_ICON})

    add_executable(${PROJECT_NAME}  ${ALL_SOURCES} ${MACOSX_BUNDLE_ICON})
    set_target_properties(${PROJECT_NAME} PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${BINARY_DIR}/info.plist)
else()

    add_library(${PROJECT_NAME} ${ALL_SOURCES})
endif()

target_include_directories(${PROJECT_NAME} PUBLIC ${BINARY_DIR})

# c++11
# ******************************************************************************
if(${TEMPLATE_USE_CPP11})
    if(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.0)
        set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
        set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
    else()
        if(APPLE)
            list(APPEND CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y -stdlib=libc++")
        endif()
        if(UNIX AND NOT APPLE)
            list(APPEND CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
        endif()
    endif()
endif()

# copy resources
# ******************************************************************************
if(APPLE)
    execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${MACOSX_BUNDLE_RESOURCES})
    execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different ${MACOSX_BUNDLE_ICON} ${MACOSX_BUNDLE_RESOURCES})
  #  add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory "${ROOT_DIR}/Resources" ${BUILD_DIR}/${PROJECT_NAME}.app/Contents/Resources)

else()
   # add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory "${ROOT_DIR}/Resources" ${BUILD_DIR}/Resources)
    #add_custom_target(CopyResources ALL COMMAND ${CMAKE_COMMAND} -E copy_directory "${ROOT_DIR}/Resources" ${BUILD_DIR}/Resources)
endif()

# link
# ******************************************************************************
target_link_libraries(${PROJECT_NAME}
    PUBLIC Qt${QT_VERSION_MAJOR}::Core
    PUBLIC Qt${QT_VERSION_MAJOR}::Widgets

    )

file(GLOB HEADER_FILES "include/MeshFix/*.h")
file(GLOB Kernel_HEADER_FILES "include/Kernel/*.h")
file(GLOB TMesh_HEADER_FILES "include/TMesh/*.h")
install(FILES ${HEADER_FILES} DESTINATION include/QMeshfix/MeshFix COMPONENT headers)
install(FILES ${Kernel_HEADER_FILES} DESTINATION include/QMeshfix/Kernel COMPONENT headers)
install(FILES ${TMesh_HEADER_FILES} DESTINATION include/QMeshfix/TMesh COMPONENT headers)
install(TARGETS QMeshFix
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

